/**
 * @(#) Client.java
 */

package TheRestaurantOwner;

public class Client
{
	private String name;
	private String surname;
	private String telephoneNumber;
	private String taxCode;
	
	private java.util.List<Order> orders;
	
	
	public Client( String name, String surname, String telephoneNumber, String taxCode )
	{
		this.name = name;
		this.surname = surname;
		this.telephoneNumber = telephoneNumber;
		this.taxCode = taxCode;
		
		orders = new java.util.ArrayList<Order>();
	}
	
	public Order makeOrder( Menu menu ){
		// From every table, we assume that
		// 2 dishes and 2 beverages are chosen randomly by the two clients.
		java.util.Random random = new java.util.Random();
		
		Beverage[] beverages = new Beverage[2];
		beverages[0] = menu.getBeverage(random.nextInt(5));
		beverages[1] = menu.getBeverage(random.nextInt(5));
		
		Dish[] dishes = new Dish[2];
		dishes[0] = menu.getDish(random.nextInt(5));
		dishes[1] = menu.getDish(random.nextInt(5));
		
		Order order = new Order(beverages, dishes);
		orders.add(order);
		return order;
	}
	
	public boolean isSatisfiedWithService( Waiter waiter )
	{
		java.util.Random random = new java.util.Random();
		int percentage = 0;
		switch (waiter.getExperience()){
		case high:
			percentage= 90;
			break;
		case medium:
			percentage= 80;
			break;
		case low:
			percentage= 60;
			break;
		}
		return random.nextInt(100) < percentage;
	}
	
	public boolean isSatisfiedWithFood( MenuItem menuItem, Employee employee, int cost )
	{
		/*
		A client is satisfied with the food 80% of times if the level of experience of the chef is high, 60% if it is
		medium, 40% if it is low. If the dish chosen is of high quality these percentages increase by 20
		percentage points.
		A client is satisfied with the beverages 80% of times if the level of experience of the barman is high,
		60% if it is medium, 40% if it is low. If the beverage chosen is of high quality these percentages increase
		by 20 percentage points.
		*/

		java.util.Random random = new java.util.Random();
		int percentage = 0;
		switch (employee.getExperience()){
		case high:
			percentage= 80;
			break;
		case medium:
			percentage= 60;
			break;
		case low:
			percentage= 40;
			break;
		}
		if (menuItem.getQuality() == ItemQuality.high){
			percentage += 20;
		}
		
		// For every 3 euros of difference between the cost of the ingredients and the cost of a dish or of a
		// beverage, these percentages decrease by 10 percentage points
		int diff = cost - menuItem.getIngredientsCost();
		percentage -= (diff / 3) * 10;
		
		// Client satisfaction cannot drop below zero,
		// and it cannot raise above 100.
		if (percentage < 0){
			percentage = 0;
		} else if (percentage > 100){
			percentage = 100;
		}
		
		return random.nextInt(100) < percentage;
	}
	
	public void calculateSatisficationPrecent( )
	{
		
	}
	
	public void calculateStatistics( Menu menu )
	{
		System.out.println("Client: " + name + " " + surname);
		
		int calorieCount = 0;
		int totalDishCount = 0;
		for (int i = 0; i < 5; i++){
			Dish dish = menu.getDish(i);
			int dishCount = 0;
			for (Order order : orders){
				Dish[] orderDishes = order.getDishes();
				if (orderDishes[0] == dish){
					dishCount++;
					calorieCount += orderDishes[0].getCalorieCount();
				} else if (orderDishes[1] == dish){
					dishCount++;
					calorieCount += orderDishes[1].getCalorieCount();
				}
			}
			totalDishCount += dishCount;
			System.out.println("Dish ordered: " + dish.getName() + " " + dishCount + " times");
		}
		
		int volumeCount = 0;
		int totalBeverageCount = 0;
		for (int i = 0; i < 5; i++){
			Beverage beverage = menu.getBeverage(i);
			int beverageCount = 0;
			for (Order order : orders){
				Beverage[] orderBeverages = order.getBeverages();
				if (orderBeverages[0] == beverage){
					beverageCount++;
					volumeCount += orderBeverages[0].getVolume();
				} else if (orderBeverages[1] == beverage){
					beverageCount++;
					volumeCount += orderBeverages[1].getVolume();
				}
			}
			totalBeverageCount += beverageCount;
			System.out.println("Beverage ordered: " + beverage.getName() + " " + beverageCount + " times");
		}
		
		float avgCalories = (float)calorieCount / totalDishCount;
		System.out.println("Average dish calories: " + new java.text.DecimalFormat("#.##").format(avgCalories));
		float avgVolume = (float)volumeCount / totalBeverageCount;
		System.out.println("Average beverage volume: " + new java.text.DecimalFormat("#.##").format(avgVolume) + " dl");
	}
}
